package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.system.domain.SysTenantModule;
import com.ruoyi.system.domain.vo.SysBusinessModuleVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysTenantVo;
import com.ruoyi.system.domain.bo.SysTenantBo;
import com.ruoyi.system.service.ISysTenantService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 租户管理Controller
 *
 * @author ruoyi
 * @date 2022-03-20
 */
@Validated
@Api(value = "租户管理控制器", tags = {"租户管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController {

    private final ISysTenantService iSysTenantService;

    /**
     * 查询租户管理列表
     */
    @ApiOperation("查询租户管理列表")
    @SaCheckPermission("system:tenant:list")
    @GetMapping("/list")
    public TableDataInfo<SysTenantVo> list(@Validated(QueryGroup.class) SysTenantBo bo, PageQuery pageQuery) {
        return iSysTenantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出租户管理列表
     */
    @ApiOperation("导出租户管理列表")
    @SaCheckPermission("system:tenant:export")
    @Log(title = "租户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysTenantBo bo, HttpServletResponse response) {
        List<SysTenantVo> list = iSysTenantService.queryList(bo);
        ExcelUtil.exportExcel(list, "租户管理", SysTenantVo.class, response);
    }

    /**
     * 获取租户管理详细信息
     */
    @ApiOperation("获取租户管理详细信息")
    @SaCheckPermission("system:tenant:query")
    @GetMapping("/{id}")
    public R<SysTenantVo> getInfo(@ApiParam("主键")
                                  @NotNull(message = "主键不能为空")
                                  @PathVariable("id") Long id) {
        return R.ok(iSysTenantService.queryById(id));
    }

    /**
     * 获取租户购买模块
     */
    @ApiOperation("获取租户购买模块")
    @GetMapping("businessModuleByTenantId/{id}")
    public R<List<SysBusinessModuleVo>> getBusinessModuleByTenantId(@ApiParam("主键")
                                                                    @NotNull(message = "主键不能为空")
                                                                    @PathVariable("id") String id) {
        return R.ok(iSysTenantService.queryBusinessModuleByTenantId(id));
    }


    @ApiOperation("新增租户购买管理")
    @RepeatSubmit()
    @SaCheckPermission("system:tenant:buyModule")
    @PostMapping("/buyModule")
    public R<Void> buyModule(@RequestBody List<SysTenantModule> tm) {
        return toAjax(iSysTenantService.insertOrUpdateBuyModule(tm) ? 1 : 0);
    }


    /**
     * 新增租户管理
     */
    @ApiOperation("新增租户管理")
    @SaCheckPermission("system:tenant:add")
    @Log(title = "租户管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTenantBo bo) {
        return toAjax(iSysTenantService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改租户管理
     */
    @ApiOperation("修改租户管理")
    @SaCheckPermission("system:tenant:edit")
    @Log(title = "租户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTenantBo bo) {
        return toAjax(iSysTenantService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除租户管理
     */
    @ApiOperation("删除租户管理")
    @SaCheckPermission("system:tenant:remove")
    @Log(title = "租户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysTenantService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
